package com.dk.service.impl;

import com.alibaba.fastjson.JSON;
import com.dk.mapper.MenuMapper;
import com.dk.pojo.Menu;
import com.dk.pojo.MenuExample;
import com.dk.pojo.TPerson;
import com.dk.service.MenuServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@com.alibaba.dubbo.config.annotation.Service //dubbo生产者暴露接口 alibaba包
public class MenuServiceImpl implements MenuServiceI {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Map<String, Object>> getTSource(TPerson tPerson) {

        String Menus = "menus:"+tPerson.getPid();
        //先判断redis是否有key,如果没有这个key的话
        if(!redisTemplate.hasKey(Menus)){
            List<Menu> menus = menuMapper.selectById(tPerson.getPid());
            for(Menu list : menus){
                redisTemplate.boundHashOps(Menus).put(list.getId(), JSON.toJSONString(list));

                redisTemplate.expire(Menus,1, TimeUnit.MINUTES);
            }
            List<Map<String, Object>> tree = getTree(menus, 0);
            return tree;
        }else {
            List<Object> values = redisTemplate.boundHashOps(Menus).values();
            List<Menu> menus = new ArrayList<>();
            for (Object s : values){
                menus.add(JSON.parseObject((String)s, Menu.class));
            }
            List<Map<String, Object>> tree = getTree(menus, 0);
            return tree;
        }

    }

    public List<Map<String, Object>> getTree(List<Menu> menus, Integer pid) {
        List<Map<String, Object>> listTree = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {

            Menu menu = menus.get(i);

            Map<String, Object> map = null;
            if (menu.getParentid().equals(pid)) {
                map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("text", menu.getText());
                map.put("url", menu.getUrl());
                map.put("icon", menu.getIcon());
                map.put("nodes", getTree(menus, menu.getId()));
            }
            if (map != null) {
                List<Map<String, Object>> li = (List<Map<String, Object>>) map.get("nodes");
                if (li.size() == 0) {
                    map.remove("nodes");
                }
                listTree.add(map);
            }
        }
        return listTree;
    }

    //ztree
    @Override
    public List<Map<String, Object>> getzTree() {
        List<Menu> menus = menuMapper.selectByExample(new MenuExample());
        List<Map<String, Object>> tree = getzTree(menus, 0);
        return tree;
    }

    public List<Map<String, Object>> getzTree(List<Menu> menus, Integer pid) {
        List<Map<String, Object>> listTree = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {

            Menu menu = menus.get(i);

            Map<String, Object> map = null;
            if (menu.getParentid().equals(pid)) {
                map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("name", menu.getText());
                /*map.put("url", menu.getUrl());*/
                map.put("icon", menu.getIcon());
                map.put("children", getzTree(menus, menu.getId()));
            }
            if (map != null) {
                List<Map<String, Object>> li = (List<Map<String, Object>>) map.get("children");
                if (li.size() == 0) {
                    map.remove("children");
                }
                listTree.add(map);
            }
        }
        return listTree;
    }

    @Override
    //事物管理(注解)
    @Transactional
    public void addInfos(Menu menu,TPerson tPerson) {
        String Menus = "menus:"+tPerson.getPid();
        menuMapper.insertSelective(menu);
        redisTemplate.delete(Menus);
    }

    @Override
    public Menu getUpdateInfo(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateInfo(Menu menu,TPerson tPerson) {
        String Menus = "menus:"+tPerson.getPid();
        menuMapper.updateByPrimaryKeySelective(menu);
        redisTemplate.delete(Menus);
    }

    @Override
    public void deleteInfo(Integer id,TPerson tPerson) {
        String Menus = "menus:"+tPerson.getPid();
        menuMapper.deleteByPrimaryKey(id);
        redisTemplate.delete(Menus);
    }

    //根据角色id 去查询应有的权限
    @Override
    public List<Map<String, Object>> getzCheckedTree(Integer roleId) {
        List<Menu> menus = menuMapper.selectByExample(new MenuExample());
        List<Integer> rid = menuMapper.getzCheckedZTree(roleId);
        List<Map<String, Object>> tree = getzCheckedTree(menus,0,rid);
        return tree;
    }

    public List<Map<String,Object>> getzCheckedTree(List<Menu> menus, Integer pid, List<Integer> rid) {
        List<Map<String, Object>> listTree = new ArrayList<>();
        for (int i = 0; i < menus.size(); i++) {

            Menu menu = menus.get(i);

            Map<String, Object> map = null;
            if (menu.getParentid().equals(pid)) {
                map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("name", menu.getText());
                map.put("url", menu.getUrl());
                map.put("icon", menu.getIcon());
                if(rid.contains(menu.getId())){
                    map.put("checked",true);
                }
                map.put("children", getzCheckedTree(menus, menu.getId(),rid));
            }
            if (map != null) {
                List<Map<String, Object>> li = (List<Map<String, Object>>) map.get("children");
                if (li.size() == 0) {
                    map.remove("children");
                }
                listTree.add(map);
            }
        }

        return listTree;
    }

    @Override
    public List<String> getPermsId(Integer pid) {
        return menuMapper.getPermsId(pid);
    }
}
